package com.gomo;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import com.gomo.R;

public class HomeActivity extends Activity {

	private AlertDialog alert;
	private AlertDialog alertLogout;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home);
		createExitAlert();
		createLogoutAlert();
	}

	@Override
	public void finish() {
		alert.show();
	}

	private void clearRemember() {
		SharedPreferences appSettings;
		appSettings = getSharedPreferences("GomoAppPreferences", MODE_PRIVATE);

		SharedPreferences.Editor prefEditor = appSettings.edit();
		prefEditor.putString("userEmail", "");
		prefEditor.putString("userPin", "");
		prefEditor.putBoolean("isRemembered", false);

		prefEditor.commit();
	}

	public void emailus() {
		try {
			String[] recipients = new String[] { getString(R.string.contact_email_address) };
			final Intent emailIntent = new Intent(
					android.content.Intent.ACTION_SEND);
			/* Fill it with Data */
			emailIntent.setType("plain/text");
			emailIntent
					.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);
			emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
					getString(R.string.contact_email_subject));
			emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "");
			startActivity(Intent.createChooser(emailIntent, "Send mail..."));
			// finish();

		} catch (ActivityNotFoundException e) {
			GlobalService.HandleException(e);
		}
	}

	public void buttonClick_r1b1(View view) {
		//Intent myIntent = new Intent(getBaseContext(), CcAddActivity.class);
		//startActivityForResult(myIntent, 0);
		
		Intent myIntent = new Intent(getBaseContext(), CcListActivity.class);
		startActivityForResult(myIntent, 0);
	}

	public void buttonClick_r3b2(View view) {
		emailus();
	}

	public void buttonClick_r3b3(View view) {
		Intent myIntent = new Intent(getBaseContext(), EditUserActivity.class);
		startActivityForResult(myIntent, 0);
	}

	public void buttonHomeExitClick(View view) {
		alertLogout.show();
	}

	private void createExitAlert() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Are you sure you want to exit?")
				.setCancelable(false)
				.setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								HomeActivity.super.finish();
							}
						})
				.setNegativeButton("No", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				});
		alert = builder.create();
	}

	private void createLogoutAlert() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(
				"Are you sure you want to logout, this will wipe out your saved login information?")
				.setCancelable(false)
				.setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								clearRemember();
								HomeActivity.super.finish();
							}
						})
				.setNegativeButton("No", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				});
		alertLogout = builder.create();
	}

}
